package org.example.controller;

import cn.hutool.core.util.IdUtil;
import cn.hutool.http.HttpUtil;
import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.rocketmq.client.producer.SendCallback;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.example.config.EmailService;
import org.example.config.PdfDownloadUtil;
import org.example.config.R;
import org.example.config.ZipUtils;
import org.example.domain.Logs;
import org.example.service.LogsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.messaging.support.GenericMessage;
import org.springframework.web.bind.annotation.*;

import javax.mail.MessagingException;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;

@RestController
@RequestMapping("logs")
public class LogsController {
    @Autowired
    LogsService logsService;

    @RequestMapping("delAll")
    public R delAll(@RequestParam("ids") Integer[] ids){
        System.out.println("======="+ids);
        return logsService.delAll(ids);
    }

    @RequestMapping("LogsdelById")
    public R LogsdelById(Integer id){
        return logsService.LogsdelById(id);
    }

    @RequestMapping("logsList")
    public R logsList(Page page, @RequestBody Logs logs){
        PageHelper.startPage((int) page.getCurrent(), (int) page.getSize());
        List<Logs> list = logsService.logsList(logs);
        PageInfo<Logs> logsPageInfo = new PageInfo<>(list);
        return R.successed(logsPageInfo);
    }
    @Autowired
    RocketMQTemplate rocketMQTemplate;
    @RequestMapping("delById")
    public R delById(Integer id){
        GenericMessage<String> message = new GenericMessage<>("删除成功");
        rocketMQTemplate.asyncSend("tpc-del", message, new SendCallback() {
            @Override
            public void onSuccess(SendResult sendResult) {
                String success = "发送成功";
                System.out.println(success);
            }

            @Override
            public void onException(Throwable throwable) {
                String error = "发送失败";
                System.out.println(error);
            }
        });
        return logsService.delById(id);
    }


    @Autowired
    @Qualifier("downloadPull")
    ExecutorService myThread;
    @Autowired
    EmailService emailService;
    @RequestMapping("download")
    public void download(){

        List<Logs> list = logsService.list();
        String nextIdStr = IdUtil.getSnowflake().nextIdStr();
        CountDownLatch countDownLatch = new CountDownLatch(2);
        CountDownLatch countDownLatch2 = new CountDownLatch(1);
        myThread.execute(()->{
            EasyExcel.write("F:/testtest/"+nextIdStr+".xlsx",Logs.class).sheet("sheet1").doWrite(list);
            countDownLatch.countDown();
        });
        myThread.execute(()->{
            try {
                PdfDownloadUtil.downloadPdf("D:/keystore/pdf/sign/dick.pdf","F:/testtest/dick.pdf");
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
            countDownLatch.countDown();
        });
        myThread.execute(()->{
            try {
                FileOutputStream fileOutputStream = new FileOutputStream(new File("F:/test.zip"));
                ZipUtils.toZip("F:/testtest",fileOutputStream,true);
                countDownLatch.await();
            } catch (InterruptedException | FileNotFoundException e) {
                throw new RuntimeException(e);
            }
            countDownLatch2.countDown();
        });
        myThread.execute(()->{
            try {
                emailService.sendMail("2161327843@qq.com","F:/test.zip");
                countDownLatch2.await();
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            } catch (MessagingException e) {
                throw new RuntimeException(e);
            }
        });
    }

    @GetMapping("giteeTest1")
    public R giteeTest1(){
        String url = "https://gitee.com/api/v5/user/keys";
        HashMap<String, Object> hashMap = new HashMap<>();
        hashMap.put("access_token","5a0794b94474a4a0e57b70d5f80c8737");
        hashMap.put("page",1);
        hashMap.put("per_page",20);
        String result = HttpUtil.get(url, hashMap);
        System.out.println(result);
        return R.successed(result);
    }
    @GetMapping("giteeTest2")
    public R giteeTest2(){
        String url = "https://gitee.com/api/v5/user";
        HashMap<String, Object> hashMap = new HashMap<>();
        hashMap.put("access_token","5a0794b94474a4a0e57b70d5f80c8737");
        String result = HttpUtil.get(url, hashMap);
        System.out.println(result);
        return R.successed(result);
    }
    @GetMapping("giteeTest3")
    public R giteeTest3(){
        String url = " https://gitee.com/api/v5/user/following";
        HashMap<String, Object> hashMap = new HashMap<>();
        hashMap.put("access_token","5a0794b94474a4a0e57b70d5f80c8737");
        hashMap.put("page",1);
        hashMap.put("per_page",20);
        String result = HttpUtil.get(url, hashMap);
        System.out.println(result);
        return R.successed(result);
    }

    @RequestMapping("testLogin")
    public R testLogin(){
        String url = "https://user.sdunict.com:9999/prod-api/auth/oauth/token";
        HashMap<String, Object> hashMap = new HashMap<>();
        hashMap.put("Content-Type","multipart");
        hashMap.put("grant_type","client_credentials");
        hashMap.put("client_id","GRIajPEr");
        hashMap.put("client_secret","322ddf8d92be9afd13ec657f360d513274fb7f4e");
        String result = HttpUtil.post(url, hashMap);
        return R.successed(result);
    }

    public static void main(String[] args) {
        String url = "https://user.sdunict.com:9999/prod-api/auth/oauth/token";
        HashMap<String, Object> hashMap = new HashMap<>();
        hashMap.put("Content-Type","multipart");
        hashMap.put("grant_type","client_credentials");
        hashMap.put("client_id","GRIajPEr");
        hashMap.put("client_secret","322ddf8d92be9afd13ec657f360d513274fb7f4e");
        String result = HttpUtil.post(url, hashMap);
        System.out.println(result);
    }
}
